package com.yupi.springbootinit.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * excle相关工具
 */
@Slf4j
public class ExcelUtils {

    /**
     * excel 转 csv
     *
     * @param multipartFile
     * @return
     */
    public static String excelToCsv(MultipartFile multipartFile) {

        List<Map<Integer, String>> list = null;
        ExcelTypeEnum fileExcelTypeEnum = getFileExcelTypeEnum(multipartFile);
        try {
            list = EasyExcel.read(multipartFile.getInputStream()).excelType(fileExcelTypeEnum).sheet().headRowNumber(0).doReadSync();
        } catch (IOException e) {
            log.error("数据处理错误", e);
        }

        //获取表头
        if (CollUtil.isEmpty(list)) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        LinkedHashMap<Integer, String> headerMap = (LinkedHashMap<Integer, String>) list.get(0);
        List<String> headerList = headerMap.values().stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        stringBuilder.append(StringUtils.join(headerList, ",")).append("\n");


        //获取数据
        for (int i = 1; i < list.size(); i++) {
            LinkedHashMap<Integer, String> dataMap = (LinkedHashMap<Integer, String>) list.get(i);
            List<String> dataList = dataMap.values().stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            stringBuilder.append(StringUtils.join(dataList, ",")).append("\n");
        }

        return stringBuilder.toString();
    }

    public static List<Map<Integer, String>> excelToTable(MultipartFile multipartFile) {
        List<Map<Integer, String>> list = null;
        ExcelTypeEnum fileExcelTypeEnum = getFileExcelTypeEnum(multipartFile);
        try {
            list = EasyExcel.read(multipartFile.getInputStream()).excelType(fileExcelTypeEnum).sheet().headRowNumber(0).doReadSync();
        } catch (IOException e) {
            log.error("数据处理错误", e);
        }
        //获取表头
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<Map<Integer, String>>();
        }

        return list;
    }

    private static ExcelTypeEnum getFileExcelTypeEnum(MultipartFile multipartFile){
        String suffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        ExcelTypeEnum excelTypeEnum = ExcelTypeEnum.CSV;
        if ("xlsx".equals(suffix)) {
            excelTypeEnum = ExcelTypeEnum.XLSX;
        } else if ("xls".equals(suffix)) {
            excelTypeEnum = ExcelTypeEnum.XLS;
        }
        return excelTypeEnum;
    }
    public static String excelToCsvTableTest(long chartId) {


        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:网站数据.xlsx");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<Map<Integer, String>> list = null;
        list = EasyExcel.read(file).excelType(ExcelTypeEnum.XLSX).sheet().headRowNumber(0).doReadSync();


        //获取表头
        if (CollUtil.isEmpty(list)) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        LinkedHashMap<Integer, String> headerMap = (LinkedHashMap<Integer, String>) list.get(0);
        List<String> headerList = headerMap.values().stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        stringBuilder.append(StringUtils.join(headerList, ",")).append("\n");


        //获取数据
        for (int i = 1; i < list.size(); i++) {
            LinkedHashMap<Integer, String> dataMap = (LinkedHashMap<Integer, String>) list.get(i);
            List<String> dataList = dataMap.values().stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            stringBuilder.append(StringUtils.join(dataList, ",")).append("\n");
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String csvString = excelToCsvTableTest(1);
        System.out.println(csvString);


    }
}
